<template>
  <div>
    <div id="bottomRightChart" style="width:100%;height:6.175rem;"></div>
  </div>
</template>

<script>
import echartMixins from '@/utils/resizeMixins'
import { createNamespacedHelpers } from 'vuex'
const { mapState } = createNamespacedHelpers('screen')

export default {
  data () {
    return {

    }
  },
  mounted () {
    this.makeSource()
    this.draw()
  },
  mixins: [echartMixins],
  computed: {
    ...mapState(['screendata'])
  },
  watch: {
    'screendata.everydaymonthdata': {
      deep: true,
      handler () {
        this.makeSource()
        this._chart.setOption({
          dataset: {
            source: _.cloneDeep(this.source)
          }
        })
      }
    }
  },
  methods: {
    makeSource () {
      const datalist = this.screendata.everydaymonthdata
      const dimensions = _.flatten(datalist.map(d => _.keys(d)))
      const source = []
      datalist.forEach((list, k) => {
        const data = list[dimensions[k]]
        data.forEach((d, i) => {
          if (k === 0) {
            const item = []
            item.push(d.aday, d.elec)
            source.push(item)
          } else {
            source[i].push(d.elec)
          }
        })
      })
      source.unshift(['time'].concat(dimensions))
      this.source = source
    },
    draw () {
      // 基于准备好的dom，初始化echarts实例
      this._chart = this.$echarts.init(document.getElementById('bottomRightChart'))
      //  ----------------------------------------------------------------
      const option = {
        title: {
          text: '',
          x: 'center',
          y: 0,
          textStyle: {
            color: '#B4B4B4',
            fontSize: 16,
            fontWeight: 'normal'
          }
        },
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(255,255,255,0.1)',
          axisPointer: {
            type: 'shadow',
            label: {
              show: true,
              backgroundColor: '#7B7DDC'
            }
          }
        },
        legend: {
          // data: ['已贯通', '计划贯通', '贯通率'],
          show: true,
          textStyle: {
            color: '#B4B4B4'
          },
          top: '0%'
        },
        grid: {
          width: '86%',
          top: 30,
          bottom: 40
        },
        xAxis: {
          type: 'category',
          axisLine: {
            lineStyle: {
              color: '#B4B4B4'
            }
          },
          axisTick: {
            show: false
          }
        },
        yAxis: {
          splitLine: { show: false },
          axisLine: {
            lineStyle: {
              color: '#B4B4B4'
            }
          },
          axisLabel: {
            formatter: '{value} '
          }
        },
        series: [
          {
            name: this.$t('bnepF54rxOycmRxbgvlpv'),
            type: 'bar',
            // seriesLayoutBy: 'row',
            barWidth: 10,
            stack: '1',
            itemStyle: {
              normal: {
                barBorderRadius: 5,
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: "rgba(156,107,211,0.8)" },
                  { offset: 0.2, color: "rgba(156,107,211,0.5)" },
                  { offset: 1, color: "rgba(156,107,211,0.2)" }
                ])
              }
            }
          },
          {
            name: this.$t('9V45FBEQ4B_cug4qBzAoI'),
            type: 'bar',
            // seriesLayoutBy: 'row',
            barWidth: 10,
            stack: '1',
            itemStyle: {
              normal: {
                barBorderRadius: 5,
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: '#956FD4' },
                  { offset: 1, color: '#3EACE5' }
                ])
              }
            }
          }
        ],
        dataset: {
          source: _.cloneDeep(this.source)
        }
      }
      this._chart.setOption(option)
    }
  },
  destroyed () {
    window.onresize = null
  }
}
</script>

<style lang="scss" scoped>
</style>
